package com.masterclient.app.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.eoner.baselibrary.bean.goods.VipUpgradeBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.utils.CommonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/3/13.
 */
public class UpgradeVipLayout extends RelativeLayout {

    @BindView(R.id.upgrade_vip_bg)
    ImageView upgradeVipBg;
    @BindView(R.id.upgrade_vip_logo)
    ImageView upgradeVipLogo;
    @BindView(R.id.upgrade_vip_btn)
    TextView upgradeVipBtn;
    @BindView(R.id.upgrade_vip_right)
    ImageView upgradeVipRight;
    @BindView(R.id.upgrade_vip_title)
    TextView upgradeVipTitle;
    @BindView(R.id.upgrade_vip_subtitle)
    TextView upgradeVipSubtitle;
    @BindView(R.id.upgrade_vip_content_layout)
    LinearLayout upgradeVipContentLayout;
    @BindView(R.id.upgrade_vip_btn_layout)
    LinearLayout upgradeVipBtnLayout;

    private Context context;
    private String url, source;
    private UpgradeVipClick upgradeVipClick;

    public void setUpgradeVipClick(UpgradeVipClick upgradeVipClick) {
        this.upgradeVipClick = upgradeVipClick;
    }

    public interface UpgradeVipClick{
        void onUpgradeVip(String upgradeVipUrl);
    }

    public UpgradeVipLayout(Context context) {
        super(context, null);
    }

    public UpgradeVipLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        inflate(context, R.layout.layout_upgrade_vip, this);
        ButterKnife.bind(this);
    }

    public void setData(VipUpgradeBean vipUpgradeBean, boolean isRight, String source){
        this.source = source;
        this.url = vipUpgradeBean.getUrl();
        if (isRight){
            upgradeVipRight.setVisibility(VISIBLE);
            upgradeVipBtn.setBackgroundResource(R.color.transparent);
            ((LayoutParams)upgradeVipContentLayout.getLayoutParams()).leftMargin = ScreenUtils.dp2px(48);
            upgradeVipBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
        }else {
            upgradeVipRight.setVisibility(GONE);
            upgradeVipBtn.setBackgroundResource(R.drawable.shape_round_8a4235);
            ((LayoutParams)upgradeVipContentLayout.getLayoutParams()).leftMargin = ScreenUtils.dp2px(44);
            upgradeVipBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
        }

        if (!TextUtils.isEmpty(vipUpgradeBean.getImage())) {
            Glide.with(context).load(vipUpgradeBean.getImage()).into(upgradeVipBg);
            Glide.with(context).load(vipUpgradeBean.getImage()).apply(new RequestOptions().centerInside()).into(upgradeVipLogo);
        }

        upgradeVipTitle.setText(Html.fromHtml(vipUpgradeBean.getMessage()+"<font color=#F23051>"+vipUpgradeBean.getProfit() +"</font>"));
        if (!TextUtils.isEmpty(vipUpgradeBean.getSub_message())){
            upgradeVipSubtitle.setVisibility(VISIBLE);
            upgradeVipSubtitle.setText(vipUpgradeBean.getSub_message());
            upgradeVipSubtitle.setText(Html.fromHtml(vipUpgradeBean.getSub_message()+"<font color=#F23051>"+vipUpgradeBean.getSub_profit() +"</font>"));
        }else {
            upgradeVipSubtitle.setVisibility(GONE);
        }
    }

    public void setData(VipUpgradeBean vipUpgradeBean, boolean isRight,boolean isShowRight, String source){
        this.source = source;
        this.url = vipUpgradeBean.getUrl();
        if (!isShowRight){
            upgradeVipBtnLayout.setVisibility(GONE);
        }else {
            upgradeVipBtnLayout.setVisibility(VISIBLE);
            if (isRight){
                upgradeVipRight.setVisibility(VISIBLE);
                upgradeVipBtn.setBackgroundResource(R.color.transparent);
                ((LayoutParams)upgradeVipContentLayout.getLayoutParams()).leftMargin = ScreenUtils.dp2px(48);
                upgradeVipBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
            }else {
                upgradeVipRight.setVisibility(GONE);
                upgradeVipBtn.setBackgroundResource(R.drawable.shape_round_8a4235);
                ((LayoutParams)upgradeVipContentLayout.getLayoutParams()).leftMargin = ScreenUtils.dp2px(44);
                upgradeVipBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
            }
        }
        if (!TextUtils.isEmpty(vipUpgradeBean.getImage())) {
            Glide.with(context).asBitmap().load(vipUpgradeBean.getImage())
                    .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                        @Override
                        public void onResourceReady(Bitmap resource, @Nullable Transition<? super Bitmap> glideAnimation) {
                            upgradeVipBg.setImageBitmap(resource);
                            upgradeVipLogo.setImageBitmap(resource);
                        }
                    });
//            Glide.with(context).load(vipUpgradeBean.getImage()).apply(new RequestOptions()).into(upgradeVipBg);
//            Glide.with(context).load(vipUpgradeBean.getImage()).apply(new RequestOptions().centerInside()).into(upgradeVipLogo);
        }

        upgradeVipTitle.setText(Html.fromHtml(vipUpgradeBean.getMessage()+"<font color=#F23051>"+vipUpgradeBean.getProfit() +"</font>"));
        if (!TextUtils.isEmpty(vipUpgradeBean.getSub_message())){
            upgradeVipSubtitle.setVisibility(VISIBLE);
            upgradeVipSubtitle.setText(vipUpgradeBean.getSub_message());
            upgradeVipSubtitle.setText(Html.fromHtml(vipUpgradeBean.getSub_message()+"<font color=#F23051>"+vipUpgradeBean.getSub_profit() +"</font>"));
        }else {
            upgradeVipSubtitle.setVisibility(GONE);
        }
    }

    @OnClick(R.id.upgrade_vip_btn_layout)
    public void onViewClicked() {
        if (upgradeVipClick != null) {
            upgradeVipClick.onUpgradeVip(url);
        }else {
            CommonUtil.goAnyWhere(context,url,"","","","");
        }
    }
}
